/**
 * @author yxw
 * date : 2013年11月2日 下午10:19:01 
 */
package com.tzinfo.tzt.activity;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.tzinfo.data.TztData;
import com.tzinfo.tzt.R;
import com.tzinfo.tzt.activity.util.MyApplication;
import com.tzinfo.tzt.activity.view.MyProgressDialog;
import com.tzinfo.tzt.config.AppDataConfig;
import com.tzinfo.tzt.domain.MyResultList;
import com.tzinfo.tzt.util.HttpTools;
import com.tzinfo.tzt.util.JsonTools;
import com.tzinfo.tzt.util.ValidityTools;

public class FalseInformationActivity extends BaseActivity implements
		OnClickListener {

	private Button backBtn, submitBtn;
	private Dialog dialog;
	private EditText mobileEditText, emailEditText, contentEditText;
	private CheckBox isCheck;
	private int houseid;
	private int tabNum = 2;
	private String houseName = "";
	private TextView nameTextView;
	private MyResultList result;
	private String baseUrl = AppDataConfig.oldUrl
			+ AppDataConfig.OldhouseFalsityFeedback;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_false_info_submit);
		Intent thisIntent = getIntent();
		houseid = thisIntent.getIntExtra("houseid", 0);
		tabNum = thisIntent.getIntExtra("tabNum", 2);
		houseName = thisIntent.getStringExtra("houseName");
		init();
	}

	private void init() {

		if (tabNum == 3) {
			baseUrl = AppDataConfig.zufangUrl
					+ AppDataConfig.RenthouseFalsityFeedback;
		}

		backBtn = (Button) this.findViewById(R.id.bt_back);
		backBtn.setOnClickListener(this);
		submitBtn = (Button) this.findViewById(R.id.bt_submit);
		submitBtn.setOnClickListener(this);
		nameTextView = (TextView) this.findViewById(R.id.text_apartment_name);
		nameTextView.setText(houseName);
		contentEditText = (EditText) this.findViewById(R.id.et_content);
		emailEditText = (EditText) this.findViewById(R.id.et_email);
		mobileEditText = (EditText) this.findViewById(R.id.et_mobile);

		isCheck = (CheckBox) this.findViewById(R.id.check_accept_agreement);

		dialog = new MyProgressDialog(this).createDialog(getResources()
				.getString(R.string.loading));
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bt_back:
			this.finish();
			break;
		case R.id.bt_submit:
			if (isCheck.isChecked()) {
				String mobileStr = mobileEditText.getText().toString().trim();
				String emailStr = emailEditText.getText().toString().trim();
				if (!"".equals(mobileStr)
						&& !ValidityTools.checkPhoneNum(mobileStr)) {
					Toast.makeText(this, "请正确输入手机号", 5000).show();

				} else if (!"".equals(emailStr)
						&& !ValidityTools.isValidEmail(emailStr)) {
					Toast.makeText(this, "请正确输入邮箱帐号", 5000).show();
				} else {
					// 发请求
					confirmTosubmit("确认提交吗？");
				}

			} else {
				Toast.makeText(getApplicationContext(), "请先阅读用《户服务协议》", 5000)
						.show();
			}
			break;
		default:
			break;
		}
	}

	@SuppressLint("HandlerLeak")
	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.arg1) {
			case 1:
				dialog.dismiss();
				Toast.makeText(getApplicationContext(), "提交成功，谢谢您的监督！", 5000)
						.show();
				finish();
				break;
			case -1:
				getDataError((String) msg.obj);
				break;
			}
		}
	};
	/**
	 * 线程 得到新房列表
	 */
	Runnable submitRun = new Runnable() {

		@Override
		public void run() {
			Message msg = handler.obtainMessage();
			String jsonString;
			String para = "?" + "houseid=" + houseid + "&";
			if (TztData.userInfo != null) {
				para += "sessionid="
						+ TztData.userInfo.getData().getSessionID();
			}
			String content = "";
			try {
				content = URLEncoder.encode(contentEditText.getText()
						.toString(), "UTF-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			para += "content=" + content + "&" + "mobile"
					+ mobileEditText.getText() + "&" + "email"
					+ emailEditText.getText();
			Log.wtf("url", baseUrl + para);
			jsonString = HttpTools.getJsonContent(baseUrl + para);
			Log.e("JsonString", jsonString);
			result = JsonTools.getResult(jsonString);
			if (result.getSuccess()) {
				msg.arg1 = 1;
			} else {
				msg.arg1 = -1;
				msg.obj = result.getErrorMessage();

			}
			handler.sendMessage(msg);
		}
	};

	private void getDataError(String ERRORMESSAGE) {
		new AlertDialog.Builder(this).setCancelable(false)
				.setMessage(ERRORMESSAGE)
				.setPositiveButton(getResources().getString

				(R.string.cancel), new DialogInterface.OnClickListener()

				{
					public void onClick

					(DialogInterface dialog, int which) {
					}
				}).setNegativeButton(getResources().getString

				(R.string.try_again), new DialogInterface.OnClickListener()

				{
					public void onClick(

					DialogInterface dialogInterface, int

					whichButton) {
						dialog.show();
						new Thread(submitRun).start();
					}
				}).show();
	}

	private void confirmTosubmit(String ERRORMESSAGE) {
		new AlertDialog.Builder(this).setCancelable(false)
				.setMessage(ERRORMESSAGE)
				.setPositiveButton(getResources().getString

				(R.string.cancel), new DialogInterface.OnClickListener()

				{
					public void onClick(DialogInterface dialog, int which) {
					}
				}).setNegativeButton(getResources().getString

				(R.string.sure), new DialogInterface.OnClickListener()

				{
					public void onClick(

					DialogInterface dialogInterface, int

					whichButton) {
						dialog.show();
						new Thread(submitRun).start();
					}
				}).show();
	}

}
